package vip.xiaonuo.h5.royalty.core.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.dev.modular.dict.entity.DevDict;
import vip.xiaonuo.dev.modular.dict.mapper.DevDictMapper;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 字典控制器
 */

@RestController
@Validated
public class H5Dictionary {

    @Resource
    DevDictMapper devDictMapper;

    @Operation(summary = "字典")
    @GetMapping("/h5/dictionary/get")
    public CommonResult<Map<String, Map<String, String>>> getDictionary() {
        Map<String, Map<String, String>> dictionary = new HashMap<>();
        // 定义要排除的字典的value
        List<String> exclusions = List.of("");
        // 查询所有父级字典
        QueryWrapper<DevDict> parentDictQueryWrapper = new QueryWrapper<>();
        parentDictQueryWrapper.lambda()
                .eq(DevDict::getParentId, "0")
                .eq(DevDict::getCategory, "BIZ");
        List<DevDict> orgParentDicts = devDictMapper.selectList(parentDictQueryWrapper);

        // 过滤出不需要的字典
        List<DevDict> parentDicts = orgParentDicts.stream()
                .filter(parentDict -> !exclusions.contains(parentDict.getDictValue()))
                .toList();

        // 查询所有子集字典
        List<String> parentDicIds = parentDicts.stream()
                .map(DevDict::getId)
                .toList();
        QueryWrapper<DevDict> childDictQueryWrapper = new QueryWrapper<>();
        childDictQueryWrapper.lambda()
                .in(DevDict::getParentId, parentDicIds);
        List<DevDict> childDic = devDictMapper.selectList(childDictQueryWrapper);

        for (DevDict parentDict : parentDicts) {
            Map<String, String> childMap = childDic.stream()
                    .filter(childDict -> parentDict.getId().equals(childDict.getParentId()))
                    .sorted((c1, c2) -> {
                        // 数字优先排序逻辑
                        boolean isNum1 = c1.getDictValue().matches("\\d+");
                        boolean isNum2 = c2.getDictValue().matches("\\d+");

                        if (isNum1 && isNum2) {
                            return Integer.compare(
                                    Integer.parseInt(c1.getDictValue()),
                                    Integer.parseInt(c2.getDictValue())
                            );
                        } else if (isNum1) {
                            return -1;  // 数字排前面
                        } else if (isNum2) {
                            return 1;   // 数字排前面
                        } else {
                            // 非数字按字典标签字母排序
                            return c1.getDictLabel().compareToIgnoreCase(c2.getDictLabel());
                        }
                    })
                    .collect(Collectors.toMap(
                            DevDict::getDictLabel,
                            DevDict::getDictValue,
                            (oldValue, newValue) -> oldValue,
                            LinkedHashMap::new
                    ));

            dictionary.put(parentDict.getDictLabel(), childMap);
        }
        return CommonResult.data(dictionary);

    }
}
